package net.ant.watercalc.web.controller;

import com.xiaoleilu.hutool.exceptions.ExceptionUtil;
import net.ant.watercalc.web.pojo.bo.FormData;
import net.ant.watercalc.web.pojo.vo.RespVo;
import net.ant.watercalc.web.service.ReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/report")
public class ReportController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ReportController.class);

    @Autowired
    ReportService reportService;

    @RequestMapping("/view")
    public @ResponseBody
    RespVo viewPdf(HttpServletResponse resp, @RequestBody FormData formData) {

        BASE64Encoder base64Encoder = new BASE64Encoder();
        String pdf = null;
        try {
            byte[] pdfBytes = reportService.getReportData(formData);
            pdf = base64Encoder.encode(pdfBytes);
            if (formData.isDoSend()) {
                if (reportService.sendReport(formData, pdfBytes)) {
                    return writeSuccessResp();
                } else {
                    return writeFailResp(6000, "Send failed, Please check your email address or try it later!");
                }
            }
        } catch (IOException e) {
            logger.error(ExceptionUtil.stacktraceToString(e));
            return writeFailResp(500, "Fail to generate the preview!");
        }
        return writeSuccessResp(pdf);
    }

//    @PostMapping("/pdf")
//    @ResponseBody
//    public void exportPdf(HttpServletResponse response, String data, String imgdata) {
//        response.setContentType("application/octet-stream");
//        response.setHeader("Content-Disposition", "attachment; filename=report.pdf");
//
//        BufferedInputStream input = null;
//        OutputStream output = null;
//        File file = null;
//        try {
//            String img64 = imgdata.split(",")[1];
//            byte[] imgbuffer = ImgUtil.getImageBytes(img64);
//
//            Report reportBo = new Report();
//            reportBo.setChart1(imgbuffer);
//
//            String reportDir = ResourceUtils.getURL("report").getPath();
//            File tmpDir = new File(reportDir);
//            if (!tmpDir.exists() || !tmpDir.isDirectory()) tmpDir.mkdir();
//
//            String reportPath = reportDir + "/" + System.currentTimeMillis() + ".pdf";
//            file = PdfUtil.getPdfFile(reportPath, reportBo);
//
//            input = new BufferedInputStream(new FileInputStream(file));
//            output = response.getOutputStream();
//            IOUtils.copy(input, output);
//            output.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//            logger.error(e.getMessage());
//        } finally {
//            try {
//                if (input != null) input.close();
//                if (output != null) output.close();
//                if (file != null) file.delete();
//            } catch (IOException e) {
//                e.printStackTrace();
//                logger.error(e.getMessage());
//            }
//        }
//    }
}
